/*
 * Created by the DBAccessor generator.
 * Programmer: Lennart Martens
 * Date: 28/07/2011
 * Time: 15:04:21
 */
package com.compomics.prioritizer.db.accessors;

import java.sql.*;
import java.io.*;
import java.util.*;
import com.compomics.util.db.interfaces.*;

/*
 * CVS information:
 *
 * $Revision: 1.4 $
 * $Date: 2007/07/06 09:41:53 $
 */

/**
 * This class is a generated accessor for the Instrument_to_instrument_capabilities table.
 *
 * @author DBAccessor generator class (Lennart Martens).
 */
public class Instrument_to_instrument_capabilitiesTableAccessor implements Deleteable, Retrievable, Updateable, Persistable {

	/**
	 * This variable tracks changes to the object.
	 */
	protected boolean iUpdated = false;

	/**
	 * This variable can hold generated primary key columns.
	 */
	protected Object[] iKeys = null;

	/**
	 * This variable represents the contents for the 'instrument_to_instrument_capabilitiesid' column.
	 */
	protected long iInstrument_to_instrument_capabilitiesid = Long.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'l_instrumentid' column.
	 */
	protected long iL_instrumentid = Long.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'l_instrument_capabilitiesid' column.
	 */
	protected long iL_instrument_capabilitiesid = Long.MIN_VALUE;


	/**
	 * This variable represents the key for the 'instrument_to_instrument_capabilitiesid' column.
	 */
	public static final String INSTRUMENT_TO_INSTRUMENT_CAPABILITIESID = "INSTRUMENT_TO_INSTRUMENT_CAPABILITIESID";

	/**
	 * This variable represents the key for the 'l_instrumentid' column.
	 */
	public static final String L_INSTRUMENTID = "L_INSTRUMENTID";

	/**
	 * This variable represents the key for the 'l_instrument_capabilitiesid' column.
	 */
	public static final String L_INSTRUMENT_CAPABILITIESID = "L_INSTRUMENT_CAPABILITIESID";




	/**
	 * Default constructor.
	 */
	public Instrument_to_instrument_capabilitiesTableAccessor() {
	}

	/**
	 * This constructor allows the creation of the 'Instrument_to_instrument_capabilitiesTableAccessor' object based on a set of values in the HashMap.
	 *
	 * @param	aParams	HashMap with the parameters to initialize this object with.
	 *		<i>Please use only constants defined on this class as keys in the HashMap!</i>
	 */
	public Instrument_to_instrument_capabilitiesTableAccessor(HashMap aParams) {
		if(aParams.containsKey(INSTRUMENT_TO_INSTRUMENT_CAPABILITIESID)) {
			this.iInstrument_to_instrument_capabilitiesid = ((Long)aParams.get(INSTRUMENT_TO_INSTRUMENT_CAPABILITIESID)).longValue();
		}
		if(aParams.containsKey(L_INSTRUMENTID)) {
			this.iL_instrumentid = ((Long)aParams.get(L_INSTRUMENTID)).longValue();
		}
		if(aParams.containsKey(L_INSTRUMENT_CAPABILITIESID)) {
			this.iL_instrument_capabilitiesid = ((Long)aParams.get(L_INSTRUMENT_CAPABILITIESID)).longValue();
		}
		this.iUpdated = true;
	}


	/**
	 * This constructor allows the creation of the 'Instrument_to_instrument_capabilitiesTableAccessor' object based on a resultset
	 * obtained by a 'select * from Instrument_to_instrument_capabilities' query.
	 *
	 * @param	aResultSet	ResultSet with the required columns to initialize this object with.
	 * @exception	SQLException	when the ResultSet could not be read.
	 */
	public Instrument_to_instrument_capabilitiesTableAccessor(ResultSet aResultSet) throws SQLException {
		this.iInstrument_to_instrument_capabilitiesid = aResultSet.getLong("instrument_to_instrument_capabilitiesid");
		this.iL_instrumentid = aResultSet.getLong("l_instrumentid");
		this.iL_instrument_capabilitiesid = aResultSet.getLong("l_instrument_capabilitiesid");

		this.iUpdated = true;
	}


	/**
	 * This method returns the value for the 'Instrument_to_instrument_capabilitiesid' column
	 * 
	 * @return	long	with the value for the Instrument_to_instrument_capabilitiesid column.
	 */
	public long getInstrument_to_instrument_capabilitiesid() {
		return this.iInstrument_to_instrument_capabilitiesid;
	}

	/**
	 * This method returns the value for the 'L_instrumentid' column
	 * 
	 * @return	long	with the value for the L_instrumentid column.
	 */
	public long getL_instrumentid() {
		return this.iL_instrumentid;
	}

	/**
	 * This method returns the value for the 'L_instrument_capabilitiesid' column
	 * 
	 * @return	long	with the value for the L_instrument_capabilitiesid column.
	 */
	public long getL_instrument_capabilitiesid() {
		return this.iL_instrument_capabilitiesid;
	}

	/**
	 * This method sets the value for the 'Instrument_to_instrument_capabilitiesid' column
	 * 
	 * @param	aInstrument_to_instrument_capabilitiesid	long with the value for the Instrument_to_instrument_capabilitiesid column.
	 */
	public void setInstrument_to_instrument_capabilitiesid(long aInstrument_to_instrument_capabilitiesid) {
		this.iInstrument_to_instrument_capabilitiesid = aInstrument_to_instrument_capabilitiesid;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'L_instrumentid' column
	 * 
	 * @param	aL_instrumentid	long with the value for the L_instrumentid column.
	 */
	public void setL_instrumentid(long aL_instrumentid) {
		this.iL_instrumentid = aL_instrumentid;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'L_instrument_capabilitiesid' column
	 * 
	 * @param	aL_instrument_capabilitiesid	long with the value for the L_instrument_capabilitiesid column.
	 */
	public void setL_instrument_capabilitiesid(long aL_instrument_capabilitiesid) {
		this.iL_instrument_capabilitiesid = aL_instrument_capabilitiesid;
		this.iUpdated = true;
	}



	/**
	 * This method allows the caller to delete the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int delete(Connection aConn) throws SQLException {
		PreparedStatement lStat = aConn.prepareStatement("DELETE FROM instrument_to_instrument_capabilities WHERE instrument_to_instrument_capabilitiesid = ?");
		lStat.setLong(1, iInstrument_to_instrument_capabilitiesid);
		int result = lStat.executeUpdate();
		lStat.close();
		return result;
	}


	/**
	 * This method allows the caller to read data for this
	 * object from a persistent store based on the specified keys.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public void retrieve(Connection aConn, HashMap aKeys) throws SQLException {
		// First check to see whether all PK fields are present.
		if(!aKeys.containsKey(INSTRUMENT_TO_INSTRUMENT_CAPABILITIESID)) {
			throw new IllegalArgumentException("Primary key field 'INSTRUMENT_TO_INSTRUMENT_CAPABILITIESID' is missing in HashMap!");
		} else {
			iInstrument_to_instrument_capabilitiesid = ((Long)aKeys.get(INSTRUMENT_TO_INSTRUMENT_CAPABILITIESID)).longValue();
		}
		// In getting here, we probably have all we need to continue. So let's...
		PreparedStatement lStat = aConn.prepareStatement("SELECT * FROM instrument_to_instrument_capabilities WHERE instrument_to_instrument_capabilitiesid = ?");
		lStat.setLong(1, iInstrument_to_instrument_capabilitiesid);
		ResultSet lRS = lStat.executeQuery();
		int hits = 0;
		while(lRS.next()) {
			hits++;
			iInstrument_to_instrument_capabilitiesid = lRS.getLong("instrument_to_instrument_capabilitiesid");
			iL_instrumentid = lRS.getLong("l_instrumentid");
			iL_instrument_capabilitiesid = lRS.getLong("l_instrument_capabilitiesid");
		}
		lRS.close();
		lStat.close();
		if(hits>1) {
			throw new SQLException("More than one hit found for the specified primary keys in the 'instrument_to_instrument_capabilities' table! Object is initialized to last row returned.");
		} else if(hits == 0) {
			throw new SQLException("No hits found for the specified primary keys in the 'instrument_to_instrument_capabilities' table! Object is not initialized correctly!");
		}
	}
	/**
	 * This method allows the caller to obtain a basic select for this table.
	 *
	 * @return   String with the basic select statement for this table.
	 */
	public static String getBasicSelect(){
		return "select * from instrument_to_instrument_capabilities";
	}

	/**
	 * This method allows the caller to obtain all rows for this
	 * table from a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 * @return   ArrayList<Instrument_to_instrument_capabilitiesTableAccessor>   with all entries for this table.
	 */
	public static ArrayList<Instrument_to_instrument_capabilitiesTableAccessor> retrieveAllEntries(Connection aConn) throws SQLException {
		ArrayList<Instrument_to_instrument_capabilitiesTableAccessor>  entities = new ArrayList<Instrument_to_instrument_capabilitiesTableAccessor>();
		Statement stat = aConn.createStatement();
		ResultSet rs = stat.executeQuery(getBasicSelect());
		while(rs.next()) {
			entities.add(new Instrument_to_instrument_capabilitiesTableAccessor(rs));
		}
		rs.close();
		stat.close();
		return entities;
	}



	/**
	 * This method allows the caller to update the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int update(Connection aConn) throws SQLException {
		if(!this.iUpdated) {
			return 0;
		}
		PreparedStatement lStat = aConn.prepareStatement("UPDATE instrument_to_instrument_capabilities SET instrument_to_instrument_capabilitiesid = ?, l_instrumentid = ?, l_instrument_capabilitiesid = ? WHERE instrument_to_instrument_capabilitiesid = ?");
		lStat.setLong(1, iInstrument_to_instrument_capabilitiesid);
		lStat.setLong(2, iL_instrumentid);
		lStat.setLong(3, iL_instrument_capabilitiesid);
		lStat.setLong(4, iInstrument_to_instrument_capabilitiesid);
		int result = lStat.executeUpdate();
		lStat.close();
		this.iUpdated = false;
		return result;
	}


	/**
	 * This method allows the caller to insert the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int persist(Connection aConn) throws SQLException {
		PreparedStatement lStat = aConn.prepareStatement("INSERT INTO instrument_to_instrument_capabilities (instrument_to_instrument_capabilitiesid, l_instrumentid, l_instrument_capabilitiesid) values(?, ?, ?)");
		if(iInstrument_to_instrument_capabilitiesid == Long.MIN_VALUE) {
			lStat.setNull(1, 4);
		} else {
			lStat.setLong(1, iInstrument_to_instrument_capabilitiesid);
		}
		if(iL_instrumentid == Long.MIN_VALUE) {
			lStat.setNull(2, 4);
		} else {
			lStat.setLong(2, iL_instrumentid);
		}
		if(iL_instrument_capabilitiesid == Long.MIN_VALUE) {
			lStat.setNull(3, 4);
		} else {
			lStat.setLong(3, iL_instrument_capabilitiesid);
		}
		int result = lStat.executeUpdate();

		// Retrieving the generated keys (if any).
		ResultSet lrsKeys = lStat.getGeneratedKeys();
		ResultSetMetaData lrsmKeys = lrsKeys.getMetaData();
		int colCount = lrsmKeys.getColumnCount();
		iKeys = new Object[colCount];
		while(lrsKeys.next()) {
			for(int i=0;i<iKeys.length;i++) {
				iKeys[i] = lrsKeys.getObject(i+1);
			}
		}
		lrsKeys.close();
		lStat.close();
		// Verify that we have a single, generated key.
		if(iKeys != null && iKeys.length == 1 && iKeys[0] != null) {
			// Since we have exactly one key specified, and only
			// one Primary Key column, we can infer that this was the
			// generated column, and we can therefore initialize it here.
			iInstrument_to_instrument_capabilitiesid = ((Number) iKeys[0]).longValue();
		}
		this.iUpdated = false;
		return result;
	}

	/**
	 * This method will return the automatically generated key for the insert if 
	 * one was triggered, or 'null' otherwise.
	 *
	 * @return	Object[]	with the generated keys.
	 */
	public Object[] getGeneratedKeys() {
		return this.iKeys;
	}

}